<template>
  <div class="dialog">
    <div class="dialog-con lookBillInner">
      <p>开票</p>
      <div class="invoiceContent">
        <el-form :model="ruleForm" label-position="left" :rules="rules" ref="ruleForm" label-width="80px"
                 class="demo-ruleForm" :inline="true">
          <el-form-item label="收票日期" prop="receiptDate">
            <el-date-picker
              v-model="ruleForm.receiptDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="开票日期" prop="kaipiaoDate">
            <el-date-picker
              v-model="ruleForm.kaipiaoDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="商品名称" prop="productName">
            <el-input v-model="ruleForm.productName"></el-input>
          </el-form-item>
          <el-form-item label="发票金额" prop="invoiceMoney">
            <el-input-number v-model="ruleForm.invoiceMoney" :controls="false" :min="0"
                             :precision="2"></el-input-number>
          </el-form-item>
          <el-form-item label="类型" prop="invoiceType">
            <el-select v-model="ruleForm.invoiceType" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发票号" prop="invoiceNo">
            <el-input v-model="ruleForm.invoiceNo"></el-input>
          </el-form-item>
          <el-form-item label="付款日期" prop="payDate">
            <el-date-picker
              v-model="ruleForm.payDate"
              type="date"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="不含税额" prop="noTax">
            <el-input-number v-model="ruleForm.noTax" :controls="false" :min="0" :precision="2"></el-input-number>
          </el-form-item>
          <el-form-item label="开票公司">
            <el-input v-model="row.supplyName" readonly></el-input>
          </el-form-item>
          <el-form-item label="税额" prop="hanTax">
            <el-input-number v-model="ruleForm.hanTax" :controls="false" :min="0" :precision="2"></el-input-number>
          </el-form-item>
          <el-form-item label="收票公司">
            <el-input v-model="row.companyName" readonly></el-input>
          </el-form-item>
<!--          <el-form-item label="备注">-->
<!--            <el-input v-model="ruleForm.memo" readonly></el-input>-->
<!--          </el-form-item>-->
          <el-form-item style="width:100%;text-align:center">
            <el-button type="primary" @click="submitForm('ruleForm')" size="medium">提交</el-button>
            <el-button @click="CancelBill" size="medium">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  import {localStorage} from "js/localStorage"
  export default {
    inject: ['reload'],
    data() {
      return {
        userId: '',
        ruleForm: {
          productName: '',
          noTax: '',
          invoiceType: '',
          kaipiaoDate: '',
          receiptDate: '',
          payDate: null,
          hanTax: '',
          invoiceNo: '',
          totalPrice: 0,
          kaipiaoCompany: '',
          invoiceMoney: '',
          memo:'',
        },
        rules: {
          productName: [
            {required: true, message: '请输入商品名称', trigger: 'blur'},
            {min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur'}
          ],
          /*   noTax: [
                 { required: true, message: '请输入不含税额', trigger: 'change' }
             ],
             hanTax: [
                 {required: true, message: '请输入税额', trigger: 'change' }
             ],*/
          invoiceType: [
            {required: true, message: '请选择发票类型', trigger: 'change'}
          ],
          // companyId: [
          //     {  required: true, message: '请选择收票公司', trigger: 'change' }
          // ],
          receiptDate: [
            {required: true, message: '请选择收票日期', trigger: 'change'}
          ],
          // payDate: [
          //     { required: true, message: '请选择付款日期', trigger: 'change' }
          // ],
          kaipiaoDate: [
            {required: true, message: '请选择开票日期', trigger: 'change'}
          ],
          invoiceNo: [
            {required: true, message: '请输入发票号', trigger: 'change'}
          ],
          invoiceMoney: [
            {required: true, message: '请输入发票金额', trigger: 'change'}
          ],
          momo: [
            {required: true, message: '请输入备注', trigger: 'change'}
          ]
        },
        options: [{
          value: '1',
          label: '普通发票'
        },
          {
            value: '2',
            label: '专用发票'
          }],
        companyList: [],
      }
    },
    props: {
      row: {
        type: Object,
        default: {}
      }
    },
    created() {
      // console.log(this.row,"这一行数据")
      if (!this.row.invoiceMoney) {
        this.row.invoiceMoney = 0;
      }
      this.totalPrice = this.row.totalPrice;
      this.ruleForm.invoiceMoney = this.row.totalPrice - this.row.invoiceMoney;
      if (this.row.adjustMoney) {
        this.totalPrice = this.row.adjustMoney;
        this.ruleForm.invoiceMoney = this.calculate.operation(this.row.adjustMoney, this.row.invoiceMoney, '-');
      }
      this.userId = localStorage.get('userID')
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.confirmBill()
          } else {
            return false;
          }
        });
      },
      confirmBill() {
        let invoiceMoney = Number(this.totalPrice - this.row.invoiceMoney).toFixed(2);
        if (this.ruleForm.invoiceMoney > invoiceMoney) {
          this.$message.warning("发票金额不能大于采购金额！");
          return;
        }
        let params = this.ruleForm;
        params['companyId'] = this.row.companyId;
        params['kaipiaoCompany'] = this.row.supplyName;
        params['purBillsId'] = this.row.purBillsId;
        params['userId'] = this.userId;
        let success = data => {
          if (data.code === "OK") {
            this.$message.success("添加成功")
            this.$emit("closeTip");
          } else {
            this.$message.error(data.msg);
          }
        };
        this.api.erpInvoiceAdd(params, success);
      },
      CancelBill() {
        this.$emit("closeTip")
      }
    }
  }
</script>
<style lang="scss" scoped>
  .lookBillInner {
    width: 700px;
    height: 500px;
    p {
      width: 100%;
      text-align: center;
      background: #eee;
      line-height: 45px;
    }
    .invoiceContent {
      width: 98%;
      margin: 10px 20px;
      .el-input {
        width: 220px;
      }
      .el-select {
        width: 220px;
      }
      .el-input-number {
        width: 220px;
      }
    }

  }
</style>
